package com.chance.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chance.model.entity.ApartmentInfo;
import com.chance.model.entity.FacilityInfo;
import com.chance.model.entity.GraphInfo;
import com.chance.model.entity.LabelInfo;
import com.chance.model.enums.ItemType;
import com.chance.web.app.mapper.*;
import com.chance.web.app.service.ApartmentInfoService;
import com.chance.web.app.service.ApartmentLabelService;
import com.chance.web.app.service.GraphInfoService;
import com.chance.web.app.vo.apartment.ApartmentDetailVo;
import com.chance.web.app.vo.apartment.ApartmentItemVo;
import com.chance.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Override
    public ApartmentItemVo selectByApartmentId(Long id) {

        // 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);
        // 查询公寓图片
        List<GraphInfo> graphInfos = graphInfoService.list(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.APARTMENT));
        // 查询标签列表
        List<LabelInfo> labelInfos = apartmentLabelMapper.selectByApartmentId(id);
        // 查询公寓最小租金
        BigDecimal minRent = apartmentInfoMapper.selectApartmentMinRentById(id);
        // 数据组装
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphInfos);
        apartmentItemVo.setLabelInfoList(labelInfos);
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo selectByApartmentDetailVoId(Long id) {

        // 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);
        // 查询图片信息
        List<GraphVo> graphInfos = graphInfoService.selectItemVoById(ItemType.APARTMENT,id);
        // 查询标签列表
        List<LabelInfo> labelInfos = apartmentLabelMapper.selectByApartmentId(id);
        // 查询配套列表
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectByApartmentId(id);
        // 查询最小租金
        BigDecimal minRent = apartmentInfoMapper.selectApartmentMinRentById(id);
        // 数据组装
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted() == 1);
        apartmentDetailVo.setGraphVoList(graphInfos);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setMinRent(minRent);

        return apartmentDetailVo;
    }
}




